package org.wikinews.en.wikinewsreader;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SubmitBug extends Activity implements Runnable{

	private static final String TAG = "WikinewsReader" ;
	private ProgressDialog progDialog = null;
	private Context ctx;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.submitbug);
    	Log.d(TAG, "SubmitBug.onCreate()");

    	ctx = this;
    	
        //Take care submit button
        final Button btnSubmit = (Button) findViewById(R.id.submit_button);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Log.d(TAG, "btnSubmit");
            	
            	final EditText email  = (EditText) findViewById(R.id.email_field);
            	final EditText summary = (EditText) findViewById(R.id.summary_field);
            	final EditText desc = (EditText) findViewById(R.id.description_field);
            	
            	if(email.length() > 125){
            		//toast
            		Toast toast = Toast.makeText(getApplicationContext(), "Email must be shorter than 125 characters", 3000);
            		toast.show();
            	}else if(summary.length() > 125){
            		//toast
            		Toast toast = Toast.makeText(getApplicationContext(), "Summary must be shorter than 125 characters", 3000);
            		toast.show();
            	}else if(email.length() == 0 || summary.length() == 0 || desc.length() == 0){
            		//toast
            		Toast toast = Toast.makeText(getApplicationContext(), "All fields are required", 3000);
            		toast.show();
            	}else{
            	
	            	progDialog = ProgressDialog.show(SubmitBug.this,"Please wait...", "Submitting bug ...", true);
	
	            	Thread thread = new Thread((Runnable) SubmitBug.this);
	            	thread.start();
	                Log.d(TAG, "SubmitBug.thread.Start()");
            	}
            }
        });


	}

	public void run() {
    	final EditText email  = (EditText) findViewById(R.id.email_field);
    	final EditText summary = (EditText) findViewById(R.id.summary_field);
    	final EditText desc = (EditText) findViewById(R.id.description_field);
    	sendBug(email.getText().toString(), summary.getText().toString(), desc.getText().toString());
		handler.sendEmptyMessage(0);
	}

    private void sendBug(String email, String summary, String desc){
    	@SuppressWarnings("unused")
		String res = null;
    	Log.d(TAG, "Article.getArticle()");
    	try{
        	String[][] sending = {{"mode","submitbug"},{"email",email},{"summary",summary},{"desc",desc}};
			res = NetTrick.doIt(sending, handler);
        } catch (Exception e) {
        	handler.sendEmptyMessage(11);
        	Log.e(TAG, e.getMessage());
        }
    }
    
    private Handler handler = new Handler() {
		public void handleMessage(Message msg){
    		Log.d(TAG, "Article.handler.messageHandler()");
    		if(msg.what > 10){
        		WikinewsReader.generalError(msg.what, ctx);
        	}else{
	        	progDialog.dismiss();
	        	
	        	Builder builder = new AlertDialog.Builder(SubmitBug.this);
	            builder.setTitle("Thank you");  
	            builder.setMessage("Your bug has been submitted to the developers.  They may contact you if they have further questions.");
	            builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int id) {
	                	Log.d(TAG, "Calling finish()");
	                    SubmitBug.this.finish();
	               }
	            });
	            builder.show();
        	}
    	}
    };
    
    
}
